package com.card.admin.pojo;

import java.util.ArrayList;
import java.util.List;

import com.card.user.StringParam;
import com.google.common.collect.Lists;
import com.google.gson.reflect.TypeToken;
import com.xiaoao.net.db.DBManager;
import com.xiaoao.net.db.Entity;
import com.xiaoao.net.utils.MathUtils;
import com.xiaoao.net.utils.PubFunc;
import com.xiaoao.net.utils.json.GSONUtils;

/**
 * 管理员
 * 
 * @author siyunlong
 * @version 1.0
 * @date 2013-6-27上午10:10:08
 *
 */
public class Admin {
	private int id;
	private String name;
	private String password;
	private int group;
	private String phone;
	private int gold;
	private int state;
	private int fatherId;
	//private String roomsetting;

	private List<CzLog> czLogs = Lists.newArrayList();
	private List<SellerLog> sellerLogs = Lists.newArrayList();

	private transient Entity entity;

	public Admin() {
	}

	public Admin(Entity entity) {
		this.entity = entity;
		setId(MathUtils.parseInt(entity.getKey().getName()));
		setName((String) entity.getProperty(StringParam.UserName));
		setPassword((String) entity.getProperty(StringParam.Pwd));
		setGroup(Integer.parseInt(entity.getProperty(StringParam.Group).toString()));
		setGold(PubFunc.parseInt(entity.getProperty(StringParam.Gold)));
		setPhone(PubFunc.toString(entity.getProperty(StringParam.Phone)));
		setState(PubFunc.parseInt(entity.getProperty(StringParam.State)));
		String czs = PubFunc.toString(entity.getProperty(StringParam.CzLog));
		this.fatherId = PubFunc.parseInt(entity.getProperty(StringParam.FatherId));

		this.czLogs = GSONUtils.FromJSONString(czs, new TypeToken<List<CzLog>>() {
		}.getType());
		if (this.czLogs == null)
			czLogs = Lists.newArrayList();

		String sss = PubFunc.toString(entity.getProperty(StringParam.SellerLog));
		this.sellerLogs = GSONUtils.FromJSONString(sss, new TypeToken<List<SellerLog>>() {
		}.getType());
		if (this.sellerLogs == null)
			sellerLogs = Lists.newArrayList();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getFatherId() {
		return fatherId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getGroup() {
		return group;
	}

	public void setGroup(int group) {
		this.group = group;
	}

	public Entity getEntity() {
		return entity;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public int getGold() {
		return gold;
	}

	public void setGold(int gold) {
		this.gold = gold;
		entity.setProperty(StringParam.Gold, gold);
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public boolean isFree() {
		return this.state == StringParam.State_Free;
	}

	public String getStateTtr() {
		return this.state == StringParam.State_Free ? "冻结" : "解冻";
	}

	public String getShowState() {
		return this.state == StringParam.State_Free ? "正常" : "已冻结";
	}

	public void addCzLog(int addGold, int newGold, int id) {
		czLogs.add(new CzLog(addGold, newGold, id + ""));
		if (czLogs.size() > 30) {
			czLogs.remove(0);
		}
		entity.setProperty(StringParam.CzLog, GSONUtils.ToJSONString(czLogs));
	}

	public List<CzLog> getCzLogs() {
		return czLogs;
	}

	public void addSellerLog(SellerLog log) {
		sellerLogs.add(log);
		if (sellerLogs.size() > 30) {
			sellerLogs.remove(0);
		}
		entity.setProperty(StringParam.SellerLog, GSONUtils.ToJSONString(sellerLogs));
		saveDB();
	}

	public List<SellerLog> getSellerLogs() {
		return sellerLogs;
	}

	public void saveDB() {
		DBManager.getInstance().saveOrUpdate(entity);
	}

	public String getFatherName() {
		if (fatherId > 0) {
			Admin ff = AdminManager.getInstance().getAdminById(fatherId + "");
			if (ff != null) {
				return ff.getName();
			}
		}
		return "admin";
	}
}
